﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 表格示例数据
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-10-26 
    /// </summary>
    public class TableExampleBll
    {
        private TableExampleIService service = new TableExampleService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<TableExampleEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="dictionaryTypeId">类别主键</param>
        /// <returns></returns>
        public async Task<PageResult<TableExampleEntity>> GetList(string typeId, PageInput input)
        {
            try
            {
                return await service.GetList(typeId, input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<TableExampleEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<TableExampleEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(TableExampleEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(TableExampleEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.SortCode = CommonHelper.SortCode;
                entity.RegisterDate = DateTime.Now;
                entity.Registrant = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(string id, TableExampleEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 行编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> RowEditing(TableExampleEntity entity)
        {
            try
            {
                var tableExampleEntity = await this.GetInfo(entity.Id);
                var exampleEntity = BindModelValue(tableExampleEntity, entity);
                exampleEntity.LastModifyTime = DateTime.Now;
                exampleEntity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(exampleEntity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 将实体2的值动态赋值给实体1(名称一样的属性进行赋值)
        /// </summary>
        /// <param name="entity1">实体1</param>
        /// <param name="entity2">实体2</param>
        /// <returns>赋值后的model1</returns>
        protected T1 BindModelValue<T1, T2>(T1 entity1, T2 entity2) where T1 : class where T2 : class
        {
            Type t1 = entity1.GetType();
            Type t2 = entity2.GetType();
            PropertyInfo[] property2 = t2.GetProperties();
            //排除字段
            List<string> exclude = new List<string>() { "F_Id", "F_Registrant", "F_RegisterDate", "F_SortCode", "F_Sign", "F_PostilJson", "F_PostilCount" };
            foreach (PropertyInfo p in property2)
            {
                if (exclude.Contains(p.Name)) { continue; }
                t1.GetProperty(p.Name)?.SetValue(entity1, p.GetValue(entity2, null));
            }
            return entity1;
        }
    }
}